//
//  NISecurityPasswordView.m
//  gweAD
//
//  Created by navinfo air on 2016/11/7.
//  Copyright © 2016年 mengy. All rights reserved.
//

#import "NISecurityPasswordView.h"
#import "TagDefines.h"
#import "NIToast.h"

@implementation NISecurityPasswordView{
    IBOutlet UIView *inputView;
    
    NSArray *textFieldArray;
    __weak IBOutlet UITextField *textField;
    __weak IBOutlet UILabel *desLabel;
    __weak IBOutlet UIView *contentView;
    __weak IBOutlet UITextField *pinTextField0;
    __weak IBOutlet UITextField *pinTextField1;
    __weak IBOutlet UITextField *pinTextField2;
    __weak IBOutlet UITextField *pinTextField3;
    __weak IBOutlet UITextField *pinTextField4;
    __weak IBOutlet UITextField *pinTextField5;
   
    __weak IBOutlet UILabel *settingTitleLabel;
    __weak IBOutlet UIButton *closeButton;
    __weak IBOutlet UIButton *forgotButton;
    __weak IBOutlet UIButton *changeButton;
    __weak IBOutlet UILabel *settingErrorLabel;
    __weak IBOutlet UIView *bgView;
    
    //设置模式
    NSString *password;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];

    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
    
    
    textFieldArray = @[pinTextField0,pinTextField1,pinTextField2,pinTextField3,pinTextField4,pinTextField5];
    
    textField.inputAccessoryView = inputView;
    [self setMode:SecurityPasswordViewDefault];
}

- (void)setMode:(SecurityPasswordViewMode)mode{
    _mode = mode;
    if (_mode == SecurityPasswordViewDefault) {
        settingTitleLabel.hidden = YES;
        closeButton.hidden = NO;
        changeButton.hidden = NO;
        settingErrorLabel.hidden = YES;
        forgotButton.hidden = NO;
        bgView.hidden = NO;
    }else{
        settingTitleLabel.hidden = NO;
        closeButton.hidden = YES;
        forgotButton.hidden = YES;
        changeButton.hidden = YES;
        settingErrorLabel.hidden = YES;
        bgView.hidden = YES;
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

- (void)setHidden:(BOOL)hidden{
    [super setHidden:hidden];
    if (!hidden) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
         NSArray *viewTags = @[@TAG_NOTIFICATIONINWINDOW,@TAG_MENUINWINDOW,@TAG_SECURITYPASSWORDINWINDOW,@TAG_SECURITYPASSWORDBGINWINDOW,@TAG_LOADINGINWINDOW];
        NSNumber *number;
        for (int i = 0; i < viewTags.count; i++) {
            number = [viewTags objectAtIndex:i];
            UIView *view = [window viewWithTag:number.integerValue];
            
            
            if (view) {
                [view.superview bringSubviewToFront:view];
                [window setNeedsLayout];
                [window layoutIfNeeded];
            }
        }
    }
    
    if (self.tag != TAG_SECURITYPASSWORDINWINDOW) {
        [self setTag:TAG_SECURITYPASSWORDINWINDOW];
    }
    if (hidden) {
        [self clearPassword];
        [self closeKeyBoard];
    }else{
        [self showKeyBoard];
    }
}

- (void)closeKeyBoard{
    [textField resignFirstResponder];
}

- (void)showKeyBoard{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [textField becomeFirstResponder];
    });
}

- (void)clearPassword{
    textField.text = @"";
    pinTextField0.text = @"";
    pinTextField1.text = @"";
    pinTextField2.text = @"";
    pinTextField3.text = @"";
    pinTextField4.text = @"";
    pinTextField5.text = @"";
}

- (NSString *)text{
    NSString *text = @"";
    NSString *tmpText;
    for (int i = 0; i < textFieldArray.count; i++) {
        tmpText = ((UITextField *)[textFieldArray objectAtIndex:i]).text;
        if (tmpText && ![tmpText isEqualToString:@""]) {
            text = [NSString stringWithFormat:@"%@%@",text,tmpText];
        }
    }
    return text;
}

- (void)showError:(NSString *)errorString{
    settingErrorLabel.text = errorString;
    settingErrorLabel.hidden = NO;
}

- (void)hiddenChangeButton:(BOOL)hidden{
    changeButton.hidden = hidden;
}

-(void)setTitleLabelText:(NSString *)title
{
    settingTitleLabel.text = title;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    settingErrorLabel.hidden = YES;
    NSInteger index = aTextField.text.length;
    if (![string isEqualToString:@""] && (index >= 6 - 1)) {
        UITextField *tmpField;
        if (index == 6 -1 ) {
            tmpField = [textFieldArray objectAtIndex:index];
            tmpField.text = string;
        }
        //输入完成
        if (self.delegate) {
            [self.delegate passwordDidInputFinish:self.text];
        }
        if (self.mode == SecurityPasswordViewSetting) {
            for (NSInteger i = 0; i < textFieldArray.count; i++) {
                tmpField = [textFieldArray objectAtIndex:i];
                tmpField.text = @"";
                if (![desLabel.text isEqualToString:@"验证六位数字密码"]) {
                    desLabel.text = @"验证六位数字密码";
                }
            }
            textField.text = @"";
            return NO;
        }
        textField.text = @"";
        [self closeKeyBoard];
        [self setHidden:YES];
        return NO;
    }else{
        if (![string isEqualToString:@""]) {
            UITextField *tmpField = [textFieldArray objectAtIndex:index];
            tmpField.text = string;
            
            for (NSInteger i = index+1; i < textFieldArray.count; i++) {
                tmpField = [textFieldArray objectAtIndex:i];
                tmpField.text = @"";
            }
        }else{
            UITextField *tmpField;
            if (index > 0) {
                tmpField = [textFieldArray objectAtIndex:(index -1 )];
            }else{
                tmpField = [textFieldArray objectAtIndex:0];
            }
            
            tmpField.text = @"";
        }
        
    }
    return YES;
}
- (IBAction)onCloseButtonClick:(id)sender {
    if (self.delegate) {
        [self.delegate viewWillClose];
    }
    [self closeKeyBoard];
    [self setHidden:YES];
}

- (IBAction)onForgotButtonClick:(id)sender {
    if (self.delegate) {
        [self setHidden:YES];
        [self.delegate forgotButtonDidClick];
    }
}


- (IBAction)onChangeButtonClick:(id)sender {
    if (self.delegate) {
        [self setHidden:YES];
        [self.delegate changeButtonDidClick];
    }
}

@end
